package com.sdry.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.base.BaseController;
import com.sdry.entity.Check;
import com.sdry.entity.CheckExample;
import com.sdry.entity.CheckExample.Criteria;
import com.sdry.entity.PartStore;
import com.sdry.entity.PartStoreExample;
import com.sdry.service.CheckServise;
import com.sdry.service.PartStoreServise;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName: CheckController
 * @Description: 盘点单
 * @author jyy
 * @date 2018年7月26日
 * @version V1.0
 */
@Controller
@RequestMapping("/check")
public class CheckController extends BaseController{

    @Resource
    private CheckServise checkServise;
    @Resource
    private PartStoreServise partStoreServise;


    /**
     * 去盘点列表页面
     * @return
     */
        @RequestMapping("/pageToCheck")
        public String pageToCheck() {

            return "/check/check";
    }
        
    /**
     * 去添加盘点单页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
    	
    	return "/check/addCheck";
    }

    /**
     * 添加盘点单
     * @param response 响应
     * @param Check 盘点单实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertCheck")
    public void insertCheck( HttpServletResponse response, Check Check) throws Exception {
    	
        //添加
        int affact = checkServise.insert(Check);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询盘点单信息
     * @param request 请求
     * @param response 响应
     * @param limit 每页记录数
     * @param offset 页面
     * @param Check 盘点单信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectCheckList")
    public void selectCheckList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "limit",required = false)int limit,
                                   @RequestParam(value = "offset",required = false)int offset,
                                   Check check) throws Exception {
        int x = 1;
        CheckExample example = new CheckExample();
        Criteria createCriteria = example.createCriteria();
        
        if(check.getData() != null && check.getData() != "") {
        	createCriteria.andDataEqualTo(check.getData()); 
        }
        
        if(check.getArea() != null && check.getArea() != "") {
        	createCriteria.andAreaEqualTo(check.getArea());
        }
        
		//统计
        int count = checkServise.countByExample(example );
        //分页查询
        List<Check> list = checkServise.selectByExampleForStartPage(example, offset-x, limit);

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", count);
        jsonObject.put("rows", list);
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据id查询盘点单信息
     * @param response 响应
     * @param id 盘点单id
     * @throws Exception 异常
     */
    @RequestMapping("/selectCheckById")
    public void selectCheckById(HttpServletResponse response, Long id) throws Exception {

        Check Check = checkServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(Check);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改盘点单信息
     * @param response 响应
     * @param Check 盘点单信息
     * @throws Exception 异常
     */
    @RequestMapping("/editCheck")
    public void editCheck( HttpServletResponse response,
                             Check Check) throws Exception {

        //修改
        int affct = checkServise.updateByPrimaryKey(Check);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除盘点单
     * @param response 响应
     * @param ids 盘点单id
     * @throws Exception 异常
     */
    @RequestMapping("/deleteCheck")
    public void deleteCheck( HttpServletResponse response, String ids) throws Exception {

        int affact =checkServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }
    
    /**
     * 查询所有库存
     * @param response 响应
     * @throws Exception 异常
     */
    @RequestMapping("/selectAllStock")
    public void selectAllStock(HttpServletResponse response) throws Exception {
    	PartStoreExample example = new PartStoreExample();
		List<PartStore> partStores = partStoreServise.selectByExample(example );
		
		JSONArray array=JSONArray.fromObject(partStores);
        ResponseUtil.write(response, array);
    }

}
